#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
using namespace std;

mutex mtx;
int shared_data = 0;

void incrementSharedData(){
    lock_guard<mutex> lock(mtx);   //构造时立即加锁，析构时自动解锁，提供最基础的锁管理，保证异常安全
    shared_data++;
    cout<<"Thread "<<this_thread::get_id()<<" shared_data "<<shared_data<<endl;
}
int main()
{
    vector<thread> threads;
    for (int i = 0; i < 10; i++){
        threads.emplace_back(incrementSharedData);
    }

    for(auto& it:threads){
        it.join();
    }

    cout<<"main thread "<<this_thread::get_id()<<" shared_data: "<<shared_data<<endl;
}